﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Reflection;

namespace PsTFSWpf
{
    /// <summary>
    /// Interaction logic for OutPsTFS.xaml
    /// </summary>
    public partial class OutPsTFS : Window
    {
        /// <summary>
        /// Ensemble d'objet à afficher
        /// </summary>
        public object PsObjectTree { get; set; }

        /// <summary>
        /// Titre des noeuds
        /// </summary>
        public string Titre { get; set; }

        /// <summary>
        /// Propriété des sous noeud
        /// </summary>
        public List<string> Properties { get; set; }

        /// <summary>
        /// Sorti de l'application
        /// </summary>
        public OutPsTFS()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            if (PsObjectTree is IList<object>)
            {
                IList<object> il = PsObjectTree as IList<object>;
                foreach (object o in il)
                {
                    PropertyInfo[] properties = o.GetType().GetProperties();
                    TreeViewItem tn = new TreeViewItem();

                    if (Titre == null && properties.Count() > 1)
                        tn.Header = properties[0].GetValue(o, null).ToString();


                    foreach (PropertyInfo property in properties)
                    {
                        try
                        {
                            if (Properties == null)
                            {
                                object obj = property.GetValue(o, null);
                                TreeViewItem tnC = new TreeViewItem();
                                tnC.Header = property.Name + " : " + obj.ToString();

                                if (Titre != null)
                                    if (property.Name == Titre)
                                        tn.Header = obj.ToString();
                                tn.Items.Add(tnC);
                            }
                            else
                            {
                                if (Properties.Contains(property.Name))
                                {
                                    object obj = property.GetValue(o, null);
                                    TreeViewItem tnC = new TreeViewItem();
                                    tnC.Header = property.Name + " : " + obj.ToString();
                                    tn.Items.Add(tnC);
                                }
                            }
                        }
                        catch
                        {
                        }
                    }
                    PsTFSTreeview.Items.Add(tn);
                }
            }
        }

        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            Filter(PsTFSTreeview.Items);
        }

        private void Filter(ItemCollection itemCollection)
        {
            if (textBox1.Text.Length > 0)
            {
                foreach (TreeViewItem p in itemCollection)
                {
                    if (p.Header.ToString().Length > textBox1.Text.Length && p.Header.ToString().Substring(0, textBox1.Text.Length) == textBox1.Text)
                        p.Visibility = System.Windows.Visibility.Visible;
                    else
                        p.Visibility = System.Windows.Visibility.Collapsed;
                }
            }
            else
            {
                foreach (TreeViewItem p in itemCollection)
                {
                   p.Visibility = System.Windows.Visibility.Visible;
                }
            }
        }
    }
}
